package route

import (
	_ "gin/chibuda/docs"
	"gin/chibuda/route"
	"github.com/gin-gonic/gin"
	"github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)
func RegisterRoutes() *gin.Engine{
	r := gin.Default()
	r.Use(cors())
	r.Static("/public", "./public")  //设置资源文件目录
	//r.Static("/admin", "./public/dist")  //设置资源文件目录
	r.GET("/doc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))  //swagger路由设置
	//cache.SetLog(r)
	route.Routes(r)
	return r
}


func cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		origin := c.Request.Header.Get("origin")
		if len(origin) == 0 {
			origin = c.Request.Header.Get("Origin")
		}
		c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "__SIGN__,__TOKEN__,Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST,DELETE,PUT")
		c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(204)
			return
		}
		c.Next()
	}
}

